local playerdatadao = require "playerdatadao"
local globaldatadao = require "globaldatadao"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local configdao = require "configdao"
local timetool = require "timetool"
local filelog = require "filelog"
local marquee = require "marquee"
local base = require "base"
local json = require "cjson"

require "enum"

json.encode_sparse_array(true,1,1)

local  CMD = {}

function CMD.ping(responsemsg)
	return {status="success"}
end

function CMD.reload(responsemsg)

	local result = nil

	local gatesvrs = configdao.get_svrs("gatesvrs")
	for gatesvr_id in pairs(gatesvrs) do
		result = msgproxy.sendrpc_reqmsgto_gatesvrd(gatesvr_id, gatesvr_id, "gmcmd", "reload")
		if not result then
			return {status="gatesvr_id "..gatesvr_id.." reload failed"}
		end
	end

	--[[local matchsvrs = configdao.get_svrs("matchsvrs")
	for matchsvr_id in pairs(matchsvrs) do
		result = msgproxy.sendrpc_reqmsgto_matchsvrd("gate_1", matchsvr_id, "gmcommand", "reload")
		if not result then
			return {status="matchsvr_id "..matchsvr_id.." reload failed"}
		end
	end]]

	local roomsvrs = configdao.get_svrs("roomsvrs")
	for roomsvr_id in pairs(roomsvrs) do
		result = msgproxy.sendrpc_reqmsgto_roomsvrd(nil, roomsvr_id, roomsvr_id, "gmcmd", "reload")
		if not result then
			return {status="roomsvr_id "..roomsvr_id.." reload failed"}
		end
	end

	result = msgproxy.sendrpc_reqmsgto_rechargesvrd("gmcmd", "reload")
	if not result then
		return {status="rechargesvrd reload failed"}
	end

	configdao.reload()
	skynet.fork(function()
		skynet.sleep(200)
		msgproxy.reload()
    end) 
	return {status="success"}
end

function CMD.marquee(responsemsg)
	local result = nil

	if not responsemsg.content then
		return {status="marquee need content"}
	end

    local eventmsg = {
		config_type = 7,
		--match_name=nil,
		--match_start_countdown=nil,        --比赛开始倒计时,时间差单位s
		--rolename=nil,   --玩家角色名
		--trigger_region_value=nil, --触发区域
		option_data=responsemsg.content,
		conditions = {},
		id = marquee.generate_id(),
    }

	local gatesvrs = configdao.get_svrs("gatesvrs")
	for gatesvr_id in pairs(gatesvrs) do
		result = msgproxy.sendrpc_noticemsgto_gatesvrd(gatesvr_id, gatesvr_id, "triggermarquee", eventmsg)
		if not result then
			return {status="gatesvr_id "..gatesvr_id.." marquee failed"}
		end
	end

	return {status="success"}
end

function CMD.player_add_coin(responsemsg)
	if responsemsg.rid == nil then
		return {status="invalid player rid"}
	end

	if not playerdatadao.is_player_exist(responsemsg.rid) then
		return {status="player rid not exist or system error"}
	end

	local status, online = playerdatadao.query_player_online(responsemsg.rid)
	if online == nil then
		return {status="system error"}		
	end

	if online.gatesvr_id ~= nil 
		and online.gatesvr_id ~= ""
		and online.gatesvr_service_address ~= nil
		and online.gatesvr_service_address ~= -1 then
		local result_data = msgproxy.sendrpc_reqmsgto_gatesvrd(online.gatesvr_id, online.gatesvr_id, "gmcmd", "player_add_coin", responsemsg)
		if result_data ~= nil 
			and type(result_data) == "table"
			and result_data.issuccess then
			return {status="success"}
		end
	end
	local money
	status, money = playerdatadao.query_player_money(responsemsg.rid)
	if money == nil then
		return {status="system error"}
	end
	msghelper:save_player_coin(money, responsemsg.rid, responsemsg.coin, EReasonChangeCurrency.CHANGE_CURRENCY_GM_ADD, "player_add_coin")
	msghelper:save_player_gemstone(money, responsemsg.rid, responsemsg.gemstone, EReasonChangeCurrency.CHANGE_CURRENCY_GM_ADD, "player_add_coin")
	return {status="success"}
end

function CMD.agentmng(responsemsg)
	if responsemsg.rid == nil 
		or responsemsg.agentlevel == nil or responsemsg.agentlevel < 0 then
		return {status="invalid params"}
	end

	if not playerdatadao.is_player_exist(responsemsg.rid) then
		return {status="player rid not exist or system error"}
	end

	local status, online = playerdatadao.query_player_online(responsemsg.rid)
	if online == nil then
		return {status="system error"}		
	end

	if online.gatesvr_id ~= nil 
		and online.gatesvr_id ~= ""
		and online.gatesvr_service_address ~= nil
		and online.gatesvr_service_address ~= -1 then
		local result_data = msgproxy.sendrpc_reqmsgto_gatesvrd(online.gatesvr_id, online.gatesvr_id, "gmcmd", "agentmng", responsemsg)
		if result_data ~= nil 
			and type(result_data) == "table"
			and result_data.issuccess then
			return {status="success"}
		end
	end
	local info
	status, info = playerdatadao.query_player_info(responsemsg.rid)
	if info == nil then
		return {status="system error"}
	end
	info.agentlevel = responsemsg.agentlevel
	playerdatadao.save_player_info("update", responsemsg.rid, info)
	return {status="success"}
end

function CMD.query_system_messages(responsemsg)
	local status, messages = globaldatadao.query_system_messages()
	filelog.sys_info("query_messages", messages)
	if status == nil then
		return {status="failed", content=messages}
	end
	return {status="success", content=messages}
end

function CMD.query_recharge_tips(responsemsg)
	local status, rechargetips = globaldatadao.query_recharge_tips()
	filelog.sys_info("query_tips", rechargetips)
	if status == nil then
		return {status="failed", content=rechargetips}
	end
	return {status="success", content=rechargetips}
end

function CMD.query_marquees(responsemsg)
	local status, marquees = globaldatadao.query_marquees()
	filelog.sys_info("query_marquees", marquees)
	if status == nil then
		return {status="failed", content=marquees}
	end
	return {status="success", content=marquees}	
end

function CMD.query_shares(responsemsg)
	local status, shares = globaldatadao.query_shares()
	filelog.sys_info("query_shares", shares)
	if status == nil then
		return {status="failed", content=shares}
	end
	return {status="success", content=shares}	
end

function CMD.save_system_message(responsemsg)
	local messages = globaldatadao.save_system_message(responsemsg.mysqlcmd, responsemsg.id, {id=responsemsg.id, content=responsemsg.content})

	return {status="success",}
end

function CMD.save_recharge_tip(responsemsg)
	local rechargetips = globaldatadao.save_recharge_tip(responsemsg.mysqlcmd, responsemsg.id, {id=responsemsg.id, content=responsemsg.content})
	return {status="success",}
end

function CMD.save_marquee(responsemsg)
	local marquees = globaldatadao.save_marquee(responsemsg.mysqlcmd, responsemsg.id, {id=responsemsg.id, content=responsemsg.content})
	return {status="success"}	
end

function CMD.save_share(responsemsg)
	local marquees = globaldatadao.save_share(responsemsg.mysqlcmd, responsemsg.id, {id=responsemsg.id, content=responsemsg.content})
	return {status="success"}	
end

function CMD.query_player_info(responsemsg)
	if responsemsg.rid == nil then
		return {status="invalid player rid"}
	end

	if not playerdatadao.is_player_exist(responsemsg.rid) then
		return {status="player rid not exist or system error"}
	end

	local status, info, money
	status, money = playerdatadao.query_player_money(responsemsg.rid)
	if money == nil then
		return {status="system error"}
	end
	status, info = playerdatadao.query_player_info(responsemsg.rid)
	if info == nil then
		return {status="system error"}
	end
	return {status="success", rid=responsemsg.rid, gemstone=money.gemstone, rolename=info.rolename}
end

return CMD